package com.gopay.common.cps.dao.wallet.tyy.impl;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.constants.wallet.tyy.WaTyyProductConstants;
import com.gopay.common.cps.dao.wallet.tyy.WaTYYProductDao;
import com.gopay.common.domain.dps.WaTyyProduct;
import com.gopay.common.util.DateUtils;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 
 * @author dps
 *
 */
@Repository("waTYYProductDao")
public class WaTYYProductDaoImpl extends BaseDAOImpl<WaTyyProduct, Integer> implements WaTYYProductDao {
	@Override
	public WaTyyProduct getProductById(String productId) {
		int pId = Integer.parseInt(productId);
		return this.find(WaTyyProduct.class, pId);
	}

	@Override
	public List<WaTyyProduct> getProductByCondition(Map<String, Object> params) {
		String hql = "from WaTyyProduct t where 1=1";
		for( Map.Entry<String, Object> entry : params.entrySet() ){
			hql += " AND "+entry.getKey()+"=:"+entry.getKey();
		}
		return this.findByHql(hql);
	}

	@Override
	public WaTyyProduct getProductByProductTerm(String productTerm) {
		StringBuffer sql = new StringBuffer();
		Map<String,Object> map = new HashMap();
		sql.append("from WaTyyProduct w where w.productTerm=:productTerm");
		map.put("productTerm",productTerm);
		List<WaTyyProduct> byHql = findByHql(sql.toString(), map);
		if(byHql!=null && byHql.size()>0){
			return byHql.get(0);
		}
		return null;
	}

	/**
	 * 查最新的在售产品
	 *
	 * @return
	 */
	@Override
	public WaTyyProduct getProductNewestOnSale() {
		StringBuffer hql = new StringBuffer();
		Date currentDate=new Date();
		String stringDate=DateUtils.format(currentDate,"yyyy-MM-dd");
		Map<String,Object> map = new HashMap();
		hql.append("from WaTyyProduct w where w.status=:status and w.productTerm=:stringDate order by to_date(w.productTerm,'yyyy-mm-dd') desc");
		map.put("status", WaTyyProductConstants.SALES_ING.value);
		map.put("stringDate", stringDate);
		List<WaTyyProduct> waTyyProductList = this.findByHql(hql.toString(),map,1,1);
		if(waTyyProductList!=null && waTyyProductList.size()>0){
			return waTyyProductList.get(0);
		}
		return null;
	}
}
